package lc2;

import org.junit.Test;

public class Ex1052 {
    class Solution {
        public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
            //基础值
            int sum = 0;
            int n = customers.length;
            int max = 0;

            for (int i = 0; i < n; i++) {
                if (grumpy[i] == 0) {
                    sum += customers[i];
                }
            }

            int lo = 0, hi = 0;

            while (hi < n) {
                if (grumpy[hi] == 0) {
                    //贪心的找个合适的起始点
                    lo++;
                    hi++;
                } else {
                    break;
                }
            }

            while (hi < n) {

                while (hi < n && hi - lo < minutes) {
                    //开窗
                    if (grumpy[hi] == 1) {
                        sum += customers[hi];
                    }
                    hi++;
                }
                max = Math.max(sum, max);

                //左边界左移
                if (grumpy[lo] == 1) {
                    sum -= customers[lo];
                }
                lo++;

            }
            return max;
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        int[] customers = new int[]{1,0,1,2,1,1,7,5};
        int[] grumpy = new int[]{0,1,0,1,0,1,0,1};
        System.out.println(s.maxSatisfied(customers, grumpy, 3));
    }
}
